home *** CD-ROM | disk | FTP | other *** search
- //
- // Main.cpp
- //
- // >>> ⌐ 1996-1997 Microsoft Corporation. All rights reserved. <<<
- //
-
- #include "ActiveXAPI.h"
- #include "App.h"
- #include "Document.h"
- #include "Menus.h"
- #include "Event.h"
- #include "Callbacks.h"
- #include "Figure.h"
- #include "ObjectInfoWin.h"
- #include "TestCallbacks.h"
-
- // the one global variable
- AppData gAppData = { NULL, NULL, NULL, NULL, false, false, false, NULL, kInvalidCursor, 0};
-
- static Boolean Initialize(void);
- static void Shutdown(void);
-
- #define DECLARE_UPP( routine, what ) \
- RoutineDescriptor routine##_upp = BUILD_ROUTINE_DESCRIPTOR( \
- upp##what##ProcInfo, routine )
-
- #define UPP( routine, what ) \
- (what##UPP) &routine##_upp
-
- int initae (void);
- static pascal OSErr
- DoAEOpenApplication
- (AppleEvent *theAppleEvent,
- AppleEvent reply,
- long refcon);
- static pascal OSErr
- DoAEQuitApplication
- (AppleEvent *theAppleEvent,
- AppleEvent reply,
- long refcon);
- static pascal OSErr
- DoAEOpenOrPrintDocuments
- (AppleEvent *theAppleEvent,
- AppleEvent reply,
- long refcon);
- static OSErr
- MissedAnyParameters
- (const AppleEvent *theAppleEvent);
-
- /*****************************************************************************
- UPP Declarations
- *****************************************************************************/
- DECLARE_UPP (DoAEOpenApplication, AEEventHandler);
- DECLARE_UPP (DoAEOpenOrPrintDocuments, AEEventHandler);
- DECLARE_UPP (DoAEQuitApplication, AEEventHandler);
-
- //
- // main
- //
- void main(void)
- {
- if (Initialize())
- {
- EventLoop();
- Shutdown();
- }
- }
-
-
- //
- // Initialize
- //
- Boolean Initialize(void)
- {
- Boolean ReturnValue = false;
- short i;
-
- // Mac Tool Initializition
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
- initae();
-
- // allocate master handles
- MaxApplZone();
- for (i=1; i<=10; ++i)
- MoreMasters();
-
- SetupMenus(false);
-
- // initialize ActiveX
- {
- #ifdef DR3_API
- AXClientInfo ClientInfo = {kAXArchCFMPPC, 0};
- AXClientCallbacks Callbacks = {nil};
-
- AXExplorerBasicCallbacks IECallbacks = { IEBasic_IEGotoNewURL, IEBasic_IELoadingStarted, IEBasic_IELoadingFinished, IEBasic_IETitleChanged,
- IEBasic_IEDocumentChanged, IEBasic_IEHandleURL };
- AXURLStreamCallbacks URLStreamCallbacks = { URLStream_OpenStream, URLStream_ReadContentType, URLStream_ReadData,
- URLStream_CloseStream, URLStream_MapURL };
-
- #else
- AXCallbacks Callbacks = {nil};
- #endif
-
- AXSiteCallbacks SiteCallbacks = { Site_RequestFocus, Site_RequestSizeChange,
- Site_OnChange, Site_AcquireContext, Site_ReleaseContext };
- Callbacks.SiteCallbacks = &SiteCallbacks;
-
- #ifdef DR3_API
- // DR3 API supports Explorer and URLStream callbacks
- Callbacks.ExplorerBasicCallbacks = &IECallbacks;
- Callbacks.URLStreamCallbacks = &URLStreamCallbacks;
-
- Callbacks.StructSize = sizeof(AXClientCallbacks) - 4;
-
- // Initialize ActiveX
- ReturnValue = ( AX_RegisterClient(&ClientInfo, &Callbacks, &gAppData.ClientID) == S_OK );
-
- #else
-
- // Initialize ActiveX
- ReturnValue = AX_Initialize(&Callbacks) == S_OK;
- #endif
- }
-
- return ReturnValue;
- }
-
- //
- // SetFocusFigure
- //
- void SetFocusFigure(FigureData* inFigure)
- {
- gAppData.FocusFigure = inFigure;
- if ( inFigure )
- SetSelectedFigure(inFigure);
-
- gAppData.MenuBarDirty = true;
- }
-
- //
- // SetSelectedFigure
- //
- void SetSelectedFigure(FigureData* inFigure)
- {
- if (inFigure != gAppData.SelectedFigure)
- {
- if (gAppData.SelectedFigure)
- InvalFigure(gAppData.SelectedFigure);
-
- gAppData.SelectedFigure = inFigure;
-
- if ( gInfoWindow )
- {
- if ( inFigure )
- SetInfoSiteID(gInfoWindow, inFigure->Site);
- else
- SetInfoSiteID(gInfoWindow, 0);
- }
-
- gAppData.MenuBarDirty = true;
- }
-
- if (inFigure)
- InvalFigure(inFigure);
- }
-
- //
- // InvalFigure
- //
- void InvalFigure(FigureData* inFigure)
- {
- GrafPtr SavePort;
- Rect RectErase = inFigure->ControlRect;
- Rect PortRect;
-
- GetPort(&SavePort);
- SetPort(inFigure->Document->Window);
-
- // may be called during a callback from a control - so offset first
- PortRect = inFigure->Document->Window->portRect;
- OffsetRect(&RectErase, PortRect.left, PortRect.top);
- InsetRect(&RectErase, BorderWidth, BorderWidth);
- InvalRect(&RectErase);
-
- SetPort(SavePort);
- }
-
- //
- // Shutdown
- //
- void Shutdown(void)
- {
- WindowRef Window;
-
- // close down all windows
- while ((Window = FrontWindow()) != NULL)
- {
- DoClose(Window);
- }
-
- // close down ActiveX
- #ifdef DR3_API
- AX_UnregisterClient(gAppData.ClientID);
- #else
- AX_Shutdown();
- #endif
- }
-
- /*****************************************************************************
- Name: initae
- Description: Install Event Handlers.
- *****************************************************************************/
- int initae (void)
- {
- long result;
- Boolean HasAppleEvents;
-
- HasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &result) ? FALSE : result != 0);
-
- if (!HasAppleEvents) return -2;
-
- /* required events */
- AEInstallEventHandler (kCoreEventClass, kAEOpenApplication,
- UPP (DoAEOpenApplication, AEEventHandler),
- 0, FALSE);
- AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
- UPP (DoAEQuitApplication, AEEventHandler),
- 0, FALSE);
- AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments,
- UPP (DoAEOpenOrPrintDocuments, AEEventHandler),
- kAEOpenDocuments, FALSE);
- AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments,
- UPP (DoAEOpenOrPrintDocuments, AEEventHandler),
- kAEPrintDocuments, FALSE);
-
-
- return noErr;
- } /* end install event handlers */
-
- /*****************************************************************************
- DoAEOpenApplication
- *****************************************************************************/
- static pascal OSErr
- DoAEOpenApplication
- (AppleEvent *theAppleEvent,
- AppleEvent reply,
- long refcon)
- {
- #pragma unused(reply, refcon)
- OSErr error;
-
- error = MissedAnyParameters (theAppleEvent);
-
- CreateDocument(&gAppData.Documents, "\pUntitled");
-
- return error;
- } /* DoAEOpenApplication */
-
- /*****************************************************************************
- DoAEQuitApplication
- *****************************************************************************/
- static pascal OSErr
- DoAEQuitApplication
- (AppleEvent *theAppleEvent,
- AppleEvent reply,
- long refcon)
- {
- #pragma unused(theAppleEvent, refcon)
- OSErr error = noErr;
-
- gAppData.Quitting = true;
-
- AEPutParamPtr (&reply, keyErrorNumber, typeShortInteger,
- (Ptr) &error, sizeof (OSErr));
- return noErr;
- } /* DoAEQuitApplication */
-
- /*****************************************************************************
- DoAEOpenOrPrintDocuments
- *****************************************************************************/
- static pascal OSErr
- DoAEOpenOrPrintDocuments
- (AppleEvent *theAppleEvent,
- AppleEvent reply,
- long refcon) /* SHOULD be kAEPrintDocuments or kAEOpenDocuments */
- {
- #pragma unused(reply, refcon)
- OSErr error;
- AEDescList docList;
- DescType actualType;
- long actualSize;
- AEKeyword actualEventID;
-
- docList.dataHandle = NULL;
-
- /*
- the following is a hack. For some reason the refcon I think SHOULD be passed in,
- (that is, the eventID) is not being passed in. Therefore, I get the eventID directly
- from the event and use it to determine if we are doing an Open or Print. [der: 4/17/95]
- */
- error = AEGetAttributePtr (theAppleEvent, keyEventIDAttr, typeType,
- &actualType, &actualEventID, sizeof (AEKeyword), &actualSize);
- /* hack ends here */
-
- error = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList, &docList);
- if (!error)
- {
- if (MissedAnyParameters (theAppleEvent) == noErr)
- {
- long itemsInList;
- error = AECountItems (&docList, &itemsInList);
- if (!error)
- {
- FSSpec theFSS;
- AEKeyword ignoredKeyWord;
- DescType ignoredType;
- Size ignoredSize;
- short index;
-
- for (index = 1; index <= itemsInList; ++index)
- {
- FInfo vFinderInfo;
-
- error = AEGetNthPtr (&docList, index, typeFSS, &ignoredKeyWord,
- &ignoredType, (Ptr) &theFSS, sizeof (FSSpec), &ignoredSize);
- if (error) break;
-
- if (FSpGetFInfo(&theFSS, &vFinderInfo) != noErr)
- break;
-
- switch (vFinderInfo.fdType)
- {
- case 'SmpC':
- {
- EventRecord Event;
-
- EventAvail(everyEvent, &Event);
- DoOpenDoc(&theFSS);
- break;
- } // FILETYPE
-
- } // switch
-
- } // for
- } // if
- } // if
-
- (void) AEDisposeDesc (&docList);
- }
- return error;
- } /* DoAEOpenDocuments */
-
-
- /*****************************************************************************
- MissedAnyParameters
- *****************************************************************************/
- static OSErr
- MissedAnyParameters
- (const AppleEvent *theAppleEvent)
- {
- OSErr error = noErr;
- DescType actualType;
- long actualSize;
-
- if (AESizeOfAttribute (theAppleEvent, keyMissedKeywordAttr, &actualType, &actualSize) != errAEDescNotFound)
- {
- error = errAEParamMissed;
- }
-
- return error;
- } /* MissedAnyParameters */
-
-